﻿using S7.Net;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System;
using System.Threading.Tasks;
using SCADA_SANEST.Models;       // cần thiết để dùng PlcDeviceStatus
using SCADA_SANEST.ViewModels;   // để dùng DeviceViewModel

namespace SCADA_SANEST.ViewModels
{
    public class DeviceStatusViewModel : BaseViewModel
    {
        public ObservableCollection<DeviceViewModel> Devices { get; set; }

        public DeviceStatusViewModel()
        {
            Devices = new ObservableCollection<DeviceViewModel>
            {
                new DeviceViewModel(new PlcDeviceStatus("PLC 1", "192.168.11.125", DeviceType.Npc1)),
                new DeviceViewModel(new PlcDeviceStatus("PLC 2", "192.168.11.126", DeviceType.Npc2)),
                new DeviceViewModel(new PlcDeviceStatus("PLC 3", "192.168.11.125", DeviceType.Hsl3)),
                new DeviceViewModel(new PlcDeviceStatus("PLC 4", "192.168.11.125", DeviceType.Hbt4)),
            };

            // Start polling tất cả PLC ngay khi ViewModel được khởi tạo
            foreach (var vm in Devices)
            {
                vm.Backend.StartPolling();
            }
        }

        // Gọi hàm này khi view đóng
        public void StopAllPolling()
        {
            foreach (var vm in Devices)
            {
                vm.Backend.StopPolling();
            }
        }
    }
}
